import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH, HEIGHT = 600, 600
ROWS, COLS = 8, 8
BLOCK_SIZE = WIDTH // COLS
FPS = 60

# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("连连看游戏")
clock = pygame.time.Clock()

# 初始化游戏板
def init_board():
    types = len(COLORS)
    num_blocks = ROWS * COLS // 2
    board = []
    block_types = []
    for i in range(num_blocks):
        block_types.extend([i % types, i % types])
    random.shuffle(block_types)
    index = 0
    for _ in range(ROWS):
        row = []
        for _ in range(COLS):
            row.append(block_types[index])
            index += 1
        board.append(row)
    return board

# 绘制游戏板
def draw_board(board):
    screen.fill(WHITE)
    for row in range(ROWS):
        for col in range(COLS):
            if board[row][col] != -1:
                pygame.draw.rect(screen, COLORS[board[row][col]],
                                 (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
                pygame.draw.rect(screen, BLACK,
                                 (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
    pygame.display.flip()

# 检查两点之间是否可以连线消除
def can_connect(board, x1, y1, x2, y2):
    if board[x1][y1] != board[x2][y2] or board[x1][y1] == -1:
        return False
    # 直接相连
    if (x1 == x2 and abs(y1 - y2) == 1) or (y1 == y2 and abs(x1 - x2) == 1):
        return True
    # 一个折点
    for i in range(ROWS):
        for j in range(COLS):
            if (i == x1 and j != y1) or (i == x2 and j != y2) or (j == y1 and i != x1) or (j == y2 and i != x2):
                if board[i][j] == -1:
                    # 检查两段是否都能连通
                    if ((i == x1 and is_straight_connect(board, x1, y1, i, j)) or
                            (i == x2 and is_straight_connect(board, x2, y2, i, j)) or
                            (j == y1 and is_straight_connect(board, x1, y1, i, j)) or
                            (j == y2 and is_straight_connect(board, x2, y2, i, j))) and \
                            ((i == x1 and is_straight_connect(board, x2, y2, i, j)) or
                             (i == x2 and is_straight_connect(board, x1, y1, i, j)) or
                             (j == y1 and is_straight_connect(board, x2, y2, i, j)) or
                             (j == y2 and is_straight_connect(board, x1, y1, i, j))):
                        return True
    return False

# 检查两点之间是否可以直线连通
def is_straight_connect(board, x1, y1, x2, y2):
    if x1 == x2:
        start, end = sorted([y1, y2])
        for y in range(start + 1, end):
            if board[x1][y] != -1:
                return False
        return True
    elif y1 == y2:
        start, end = sorted([x1, x2])
        for x in range(start + 1, end):
            if board[x][y1] != -1:
                return False
        return True
    return False

# 主游戏循环
def main():
    board = init_board()
    selected = None
    running = True
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                col = x // BLOCK_SIZE
                row = y // BLOCK_SIZE
                if selected is None:
                    if board[row][col] != -1:
                        selected = (row, col)
                else:
                    s_row, s_col = selected
                    if can_connect(board, s_row, s_col, row, col):
                        board[s_row][s_col] = -1
                        board[row][col] = -1
                    selected = None
        draw_board(board)
        # 检查是否游戏结束
        all_removed = True
        for row in board:
            for block in row:
                if block != -1:
                    all_removed = False
                    break
            if not all_removed:
                break
        if all_removed:
            print("游戏结束，你赢了！")
            running = False
    pygame.quit()

if __name__ == "__main__":
    main()